﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadVinaglue.Models;
using LeadVinaglue.Models.ViewPages;
using LeadVinaglue.Repository;
using LeadVinaglue.Utility;


namespace LeadVinaglue.Controllers
{
    public class SearchController : BaseController
    {
        // Create an instance of product
        ProductListManagementRepository repository = new ProductListManagementRepository();

        // Show all product by category/sub
        public ActionResult Index(int? page, string fb, string fv, string sort)
        {
            ProductViewModel model = new ProductViewModel();

            int pageIndex = page != null ? page.Value : 1;
            string[] rawURL = Request.RawUrl.Split('?');
            model.PageName = LeadConstant.SEARCH;
            model.FilterRawURL = rawURL[0];
            model.RawURL = string.Format("{0}?fb={1}&fv={2}", rawURL[0], fb, fv);
            model.FilterBy = fb;
            model.FilterValue = fv;
            model.SortOrder = string.IsNullOrEmpty(sort) ? "moi-nhat" : sort;
            model.PageIndex = pageIndex;
            model.PageSize = 10;
            FilterViewModel modelFiter = new FilterViewModel();
            modelFiter.LanguageSelected = "Tất cả ngôn ngữ";
            modelFiter.DocumentSelected = "Tất cả các loại";
            modelFiter.TypeSelected = "Tất cả loại file";
            if (fb == "language")
            {
                modelFiter.LanguageSelected = fv;
            }
            else if (fb == "type")
            {
                modelFiter.DocumentSelected = fv;
            }
            else if (fb == "extension")
            {
                modelFiter.TypeSelected = fv;
            }
            model.Filters = modelFiter;

            // Setup common
            SetupCommon(model);

            // Get Product model
            repository.GetModelSearch(ref model);

            return View(model);
        }
    }
}
